have jeeps use std::endian (#1315)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Sat, 10 Aug 2024 20:23:50 +0000 (14:23 -0600)
committerGitHub <noreply@github.com>
Sat, 10 Aug 2024 20:23:50 +0000 (14:23 -0600)
* have jeeps use std::endian

* detect unsupported endian platforms.

jeeps/gpsapp.cc
jeeps/gpscom.cc
jeeps/gpsutil.cc
jeeps/gpsutil.h

index d56a603e13b915ea6fd463b3879cc3411a2e353e..e810c9f3c247ffcca22d5c73cef9aceb58a3e547 100644 (file)
@@ -168,8 +168,6 @@ int32_t GPS_Init(const char* port)
 {
        int32_t ret;
 
-  (void) GPS_Util_Little();
-
   ret = GPS_A000(port);
   if (ret<0) {
     return ret;
index afa1239f5cc0af0ba3d00b0e623d911e63c5d7d3..dcc56634d2523527f1e8efda273cec0cda4b78d2 100644 (file)
@@ -44,8 +44,6 @@ int32_t GPS_Command_Off(const char* port)
   GPS_Packet tra;
   GPS_Packet rec;
 
-  GPS_Util_Little();
-
   if (!GPS_Device_On(port, &fd)) {
     return gps_errno;
   }
index 9da4a9b1d73b69c078dd37f49eba6c3661ac2dd7..d88d3e57fec1d384b208129b5fbf4dc481616fad 100644 (file)
 ** Boston, MA  02110-1301, USA.
 ********************************************************************/
 #include "jeeps/gps.h"
+#include <bit>
 #include <cstdarg>
 #include <cstdlib>
 
-static int32_t gps_endian_called = 0;
-static int32_t GPS_Little = 0;
+static_assert((std::endian::native == std::endian::little) !=
+              (std::endian::native == std::endian::big),
+              "Only big or little endian platforms are supported.");
+static constexpr bool GPS_Little = std::endian::native == std::endian::little;
 
 int32_t gps_warning = 0;
 int32_t gps_error = 0;
@@ -34,35 +37,6 @@ int32_t gps_user = 0;
 int32_t gps_show_bytes = 0;
 int32_t gps_errno = 0;
 
-/* @func GPS_Util_Little ***********************************************
-**
-** Determine endian nature of host
-**
-** @return [int32] true if little-endian
-************************************************************************/
-
-int32_t GPS_Util_Little()
-{
-  static union lb {
-    char chars[sizeof(int32_t)];
-    int32_t i;
-  }
-  data;
-
-  if (!gps_endian_called) {
-    gps_endian_called = 1;
-    data.i = 0;
-    *data.chars = '\1';
-    if (data.i == 1) {
-      GPS_Little = 1;
-    } else {
-      GPS_Little = 0;
-    }
-  }
-
-  return GPS_Little;
-}
-
 
 /* @func GPS_Util_Get_Short ********************************************
 **
@@ -78,7 +52,7 @@ US GPS_Util_Get_Short(const UC* s)
 
   p = (UC*)&ret;
 
-  if (!GPS_Little) {
+  if constexpr(!GPS_Little) {
     *p++ = *(s+1);
     *p = *s;
   } else {
@@ -105,7 +79,7 @@ void GPS_Util_Put_Short(UC* s, const US v)
 {
   const auto* p = reinterpret_cast<const UC*>(&v);
 
-  if (!GPS_Little) {
+  if constexpr(!GPS_Little) {
     *s++ = *(p+1);
     *s = *p;
   } else {
@@ -134,7 +108,7 @@ double GPS_Util_Get_Double(const UC* s)
   p = (UC*)&ret;
 
 
-  if (!GPS_Little)
+  if constexpr(!GPS_Little)
     for (i=sizeof(double)-1; i>-1; --i) {
       *p++ = s[i];
     }
@@ -164,7 +138,7 @@ void GPS_Util_Put_Double(UC* s, const double v)
 
   const auto* p = reinterpret_cast<const UC*>(&v);
 
-  if (!GPS_Little)
+  if constexpr(!GPS_Little)
     for (i=sizeof(double)-1; i>-1; --i) {
       s[i] = *p++;
     }
@@ -195,7 +169,7 @@ int32_t GPS_Util_Get_Int(const UC* s)
   p = (UC*)&ret;
 
 
-  if (!GPS_Little)
+  if constexpr(!GPS_Little)
     for (i=sizeof(int32_t)-1; i>-1; --i) {
       *p++ = s[i];
     }
@@ -225,7 +199,7 @@ void GPS_Util_Put_Int(UC* s, const int32_t v)
 
   const auto* p = reinterpret_cast<const UC*>(&v);
 
-  if (!GPS_Little)
+  if constexpr(!GPS_Little)
     for (i=sizeof(int32_t)-1; i>-1; --i) {
       s[i] = *p++;
     }
@@ -255,7 +229,7 @@ uint32_t GPS_Util_Get_Uint(const UC* s)
   p = (UC*)&ret;
 
 
-  if (!GPS_Little)
+  if constexpr(!GPS_Little)
     for (i=sizeof(uint32_t)-1; i>-1; --i) {
       *p++ = s[i];
     }
@@ -285,7 +259,7 @@ void GPS_Util_Put_Uint(UC* s, const uint32_t v)
 
   const auto* p = reinterpret_cast<const UC*>(&v);
 
-  if (!GPS_Little)
+  if constexpr(!GPS_Little)
     for (i=sizeof(uint32_t)-1; i>-1; --i) {
       s[i] = *p++;
     }
@@ -315,7 +289,7 @@ float GPS_Util_Get_Float(const UC* s)
   p = (UC*)&ret;
 
 
-  if (!GPS_Little)
+  if constexpr(!GPS_Little)
     for (i=sizeof(float)-1; i>-1; --i) {
       *p++ = s[i];
     }
@@ -345,7 +319,7 @@ void GPS_Util_Put_Float(UC* s, const float v)
 
   const auto* p = reinterpret_cast<const UC*>(&v);
 
-  if (!GPS_Little)
+  if constexpr(!GPS_Little)
     for (i=sizeof(float)-1; i>-1; --i) {
       s[i] = *p++;
     }
index 0e50f703a5aed9f84d164f8ccbb9223b17ceb317..5560eda29b5d9b18ea350b46d87cc5213f64cfa0 100644 (file)
@@ -4,8 +4,6 @@
 
 #include "jeeps/gps.h"
 
-int32_t GPS_Util_Little();
-
 US     GPS_Util_Get_Short(const UC* s);
 void   GPS_Util_Put_Short(UC* s, US v);
 int32_t GPS_Util_Get_Int(const UC* s);